import modules from './modules'
import Vue from 'vue'
//这里仅示范npm安装方式的引入，其它方式引入请看最上面【安装】部分
import Router from 'uni-simple-router'
import iGlobal from '../common/global.js'	//引入 global.js
import tui from '../common/httpRequest.js'
Vue.use(Router)
//初始化
const router = new Router({
	mode:'history',
    routes: [...modules],//路由表
	h5:{
		paramsToQuery:true
	}
});

//全局路由前置守卫
router.beforeEach((to, from, next) => {
  console.log('全局路由前置守卫',to, from.query, next,uni.getStorageSync('inviteCode'))	
  console.log(to.name,tui.isLogin())
  if(!iGlobal.isUndefined(from.query.member_id) && !iGlobal.isNull(from.query.member_id) && from.query.member_id != ''){
	  uni.setStorageSync('inviteCode',from.query.member_id)
  }
  
  console.log('全局路由前置守卫code缓存数据',uni.getStorageSync('inviteCode'))
  
  if((to.name == 'register' || to.name == 'login' || to.name == 'forgetPwd' ) && tui.isLogin()){
  	  next({
  	  	name: 'index'
  	  });
  }
  // #ifdef H5
  let that = this;
  if(iGlobal.isWeixin()){
  	let code  = tui.getQueryString('code')
  	let state = tui.getQueryString('state')
	
  	console.log('有code',code,state)
	if(state && code && to.name != 'auth'){
		next({
			name: 'auth',
			params: {
				msg: '我拦截了tab5并重定向到了路由4页面上',
				code: code,
				state:state,
				inviteCode:uni.getStorageSync('inviteCode')
			},
			NAVTYPE: 'push'
		});
	}
  } 
  // #endif  
  next()
})

// 全局路由后置守卫
router.afterEach((to, from) => {
})
export default router;